package com.myjava.managesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myjava.managesystem.common.R;
import com.myjava.managesystem.dto.DishDto;
import com.myjava.managesystem.entity.Category;
import com.myjava.managesystem.entity.Dish;
import com.myjava.managesystem.entity.DishFlavor;
import com.myjava.managesystem.entity.Employee;
import com.myjava.managesystem.service.CategoryService;
import com.myjava.managesystem.service.DishFlavorService;
import com.myjava.managesystem.service.DishService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    CategoryService categoryService;
    @Resource
    RedisTemplate redisTemplate;

    @GetMapping("/page")
    public R<IPage> getPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) String name){
        Page<Dish> p = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Dish::getName,name);
        wrapper.orderByDesc(Dish::getUpdateTime);
        IPage<Dish> iPage = dishService.page(p,wrapper);
        IPage<DishDto> dtoIPage = new Page<>();
        BeanUtils.copyProperties(iPage,dtoIPage,"records");
        List<Dish> list1 = iPage.getRecords();
        List<DishDto> list2 = new ArrayList<>();
        for(Dish d:list1){
            DishDto dishDto = new DishDto();
            LambdaQueryWrapper<Category> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Category::getId,d.getCategoryId());
            dishDto.setCategoryName(categoryService.getOne(wrapper1).getName());
            BeanUtils.copyProperties(d,dishDto);
            list2.add(dishDto);
        }
        dtoIPage.setRecords(list2);
        return R.success(dtoIPage);
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        String key = "dish_" + dishDto.getCategoryId()+"_1";
        redisTemplate.delete(key);
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        String key = "dish_" + dishDto.getCategoryId()+"_1";
        redisTemplate.delete(key);
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> getDish(@PathVariable Long id){
        return R.success(dishService.getByIdWithFlavor(id));
    }

    @GetMapping("/list")
    public R<List<DishDto>> getDishList(Dish dish){
        List<DishDto> dishDtoList = null;
        String key = "dish_" + dish.getCategoryId()+"_" + dish.getStatus();
        dishDtoList = (List<DishDto>)redisTemplate.opsForValue().get(key);
        if(dishDtoList != null){
            return R.success(dishDtoList);
        }

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        wrapper.eq(Dish::getStatus,1);
        wrapper.orderByAsc(Dish::getSort);
        List<Dish> dishList = dishService.list(wrapper);
        dishDtoList = new ArrayList<>();

        for (Dish d:dishList){
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(d,dishDto);
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,dishDto.getId());
            List<DishFlavor> list = dishFlavorService.list(wrapper1);
            dishDto.setFlavors(list);
            dishDtoList.add(dishDto);
        }
        log.info("已将"+dishDtoList.get(0).getId()+"加入缓存");
        redisTemplate.opsForValue().set(key,dishDtoList,30, TimeUnit.MINUTES);
        return R.success(dishDtoList);
    }

    @PostMapping("/status/{st}")
    public R<String> updateStatus(@PathVariable int st,@RequestParam List<Long> ids){
        dishService.updateStatusWithSetmeal(st,ids);
        return R.success("停售成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.deleteWithSetmeal(ids);
        return R.success("删除成功");
    }
}
